/**
 *	Monopolymorphism - A Java-based monopoly!
 *
 *	The final project of 
 *	Object-Oriented Programming Design of NTUCSIE
 *
 *	Authors:
 *		ChungKingSing.
 *		Qing-Cheng Li.
 */
package monopolymorphism;

import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;

public class GUI{
	
	/* 這裡是用來獨立測試GUI元件的 main function */
	
	public static void main(String[] argv){
		MainWindow win = new MainWindow(system.name);
		win.setVisible(true);
				
	}
	
	
}

/* 主視窗 */
class MainWindow extends JFrame{
	private int w = system.windowW;
	private int h = system.windowH;
	
	BufferedImage bg_img;
	
	
	
	public MainWindow(String name){
		super(name);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		/* 開新遊戲 */
		JPanel p_start = new JPanel();
		p_start.setLayout(null);
		
		/* 主選單 */
		JPanel p_main = new JPanel();
		float align = (float)0.5;
		p_main.setLayout(new BoxLayout(p_main, BoxLayout.Y_AXIS));
				
		/* 進行遊戲 */
		JPanel p_game = new JPanel();
		Map map = new Map();
		
		//開始遊戲文字設定
		gameSetting set = new gameSetting();
		JLabel player_set[] = new JLabel[4];
		JTextField jtext[] = new JTextField[4];
		ImageIcon map_icon[];
		JLabel maplabel[] = new JLabel[MapFactory.readMaps().length];
		set.setPlayer(2);
		set.setMoney(200000);
		set.setMap("NTU");
		for(int i=0;i<4;i++){
			set.setName(i,"Player "+(i+1));
		}
		//空白
		ImageIcon noicon = createImageIcon("img/bg00.png",system.name);
		JLabel nouse01 = new JLabel(noicon);
		nouse01.setAlignmentX(align);
		JLabel nouse02 = new JLabel(noicon);
		nouse02.setAlignmentX(align);		
		JLabel nouse03 = new JLabel(noicon);
		nouse03.setAlignmentX(align);		
		JLabel nouse04 = new JLabel(noicon);
		nouse04.setAlignmentX(align);	
		ImageIcon icon = createImageIcon("img/logo.png",system.name);
		JLabel logono = new JLabel(icon);
		
		
		/*這裡以下開始佈置遊戲主畫面吧！*/
		
		
		//設定區
		p_start.add(logono);
		logono.setBounds(247,20,520,75);
		
		JLabel start_text = new JLabel("開始新遊戲");
		start_text.setBounds(470,115,100,20);
		p_start.add(start_text);
		
		JLabel start_1 = new JLabel("選擇玩家數量：");
		start_1.setBounds(320,155,100,20);
		p_start.add(start_1);
		
		String[] player_num_a = {"2","3","4"};
		JComboBox player_num_select = new JComboBox(player_num_a);
		player_num_select.setSelectedIndex(0);
		player_num_select.setBounds(420,155,100,20);
		player_num_select.addActionListener(new MapSelectingListener(set,1,jtext,player_set,maplabel));
		p_start.add(player_num_select);
		
		JLabel start_2 = new JLabel("設定起始金額：");
		start_2.setBounds(320,175,100,20);
		p_start.add(start_2);
		
		String[] money_num_a = {"200000","300000","400000"};
		JComboBox money_num_select = new JComboBox(money_num_a);
		money_num_select.setSelectedIndex(0);
		money_num_select.setBounds(420,175,100,20);
		money_num_select.addActionListener(new MapSelectingListener(set,2,jtext,player_set,maplabel));
		p_start.add(money_num_select);
		
		JLabel start_3 = new JLabel("選擇遊戲地圖：");
		start_3.setBounds(320,195,100,20);
		p_start.add(start_3);
		
		String[] map_a = MapFactory.readMaps();
		map_icon = new ImageIcon[map_a.length];
		maplabel = new JLabel[map_a.length];
		for(int i=0;i<map_a.length;i++){
			map_icon[i] = createImageIcon("map/"+map_a[i]+"/small.png",system.name);
			maplabel[i] = new JLabel(map_icon[i]);
			maplabel[i].setBounds(540,155,100,100);
			if(i>0)
				maplabel[i].setVisible(false);
			p_start.add(maplabel[i]);
		}
		JComboBox map_select = new JComboBox(map_a);
		map_select.setSelectedIndex(0);
		map_select.setBounds(420,195,100,20);
		map_select.addActionListener(new MapSelectingListener(set,3,jtext,player_set,maplabel));
		p_start.add(map_select);
		
		JLabel start_4 = new JLabel("玩家設定(更改後請按Enter)：");
		start_4.setBounds(320,225,250,20);
		p_start.add(start_4);

		for(int i = 0 ; i < 4 ; i++ ){
			player_set[i] = new JLabel("玩家"+(i+1)+"名稱：");
			player_set[i].setBounds(320,245+20*i,100,20);
			if(i>1)
				player_set[i].setVisible(false);
			p_start.add(player_set[i]);
			jtext[i] = new JTextField(set.name[i]);
			jtext[i].setBounds(420,245+20*i,100,20);
			jtext[i].addActionListener(new TextListener(i,set,jtext[i]));
			if(i>1)
				jtext[i].setVisible(false);
			p_start.add(jtext[i]);
		}
		
		//返回主選單
		JButton b_return = new JButton("返回主選單");
		b_return.addActionListener(new ChangePanelListener(this,p_main,"返回主選單"));
		b_return.setBounds(320,345,100,20);
		p_start.add(b_return);
		
		JButton b_go = new JButton("開始遊戲");
		b_go.addActionListener(new NewGameListener(set,this,p_game,map));
		b_go.setBounds(540,345,100,20);
		p_start.add(b_go);
		
				
		//主選單Layout設定
		
		//LOGO
		JLabel logo = new JLabel(icon);
		logo.setAlignmentX(align);
		
		//說明文字
		JLabel text = new JLabel("請選擇選項。"); 
		text.setAlignmentX(align);
		
		//開始遊戲按鈕
		JButton b_start = new JButton("開始遊戲");
		b_start.setAlignmentX(align);
		b_start.addActionListener(new ChangePanelListener(this,p_start,"開始遊戲"));
		b_start.addMouseListener(new MouseMoveListener(text,"開始進行遊戲！","請選擇選項。"));
		
		//讀取進度
		JButton b_read = new JButton("讀取進度");
		b_read.addMouseListener(new MouseMoveListener(text,"讀取遊戲紀錄，並繼續進行遊戲！（尚未釋出）","請選擇選項。"));
		b_read.setAlignmentX(align);
		b_read.setEnabled(false);
			
		//連線對戰
		JButton b_net = new JButton("連線對戰");
		b_net.addMouseListener(new MouseMoveListener(text,"與你的好友一同享受遊戲的樂趣！（尚未釋出）","請選擇選項。"));
		b_net.setAlignmentX(align);
		b_net.setEnabled(false);
		
		//關於遊戲
		JButton b_about = new JButton("關於遊戲");
		b_about.setAlignmentX(align);
		b_about.addMouseListener(new MouseMoveListener(text,"檢視本遊戲相關資訊，包括版本、程式開發者等資訊。","請選擇選項。"));
		b_about.addActionListener(new DevListener());
		
		//結束遊戲
		JButton b_quit = new JButton("結束遊戲");
		b_quit.addMouseListener(new MouseMoveListener(text,"離開"+system.name+"，回到現實世界。","請選擇選項。"));
		b_quit.setAlignmentX(align);
		b_quit.addActionListener(new EndingListener());
		
		JPanel p02 = new JPanel();
		p02.setLayout(null);			
		
		//骰子
		ImageIcon icon2 = createImageIcon("img/bg01.png",system.name);
		JLabel logo2 = new JLabel(icon2);
		p02.add(logo2);
		
		//課本
		ImageIcon icon3 = createImageIcon("img/AJ.png",system.name);
		JLabel logo3 = new JLabel(icon3);
		p02.add(logo3);

		/* 將按鈕依序加入面板 */
		p_main.add(nouse01);
		p_main.add(logo);
		p_main.add(nouse02);
		p_main.add(b_start);
		p_main.add(b_read);
		p_main.add(b_net);
		p_main.add(b_about);
		p_main.add(b_quit);
		p_main.add(nouse03);
		p_main.add(text);
		p_main.add(nouse04);
		p_main.add(p02);
		
		logo2.setBounds(450,200,200,150);
		logo3.setBounds(370,230,119,100);
		//加入至視窗
		setContentPane(p_main);
		//add(p_main);
		
		/* 偵測螢幕大小將視窗置中 */
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation( (dim.width-w)/2 , (dim.height-h)/2 );
		setSize(w,h);
		
	}
	
	/* 把圖片當成icon用來放入JLabel的function */
	protected static ImageIcon createImageIcon(String path,String description) {
            return new ImageIcon(path, description);
    }
}

/* Listener */

/* 轉換Panel */
class ChangePanelListener implements ActionListener{
	private JFrame f;
	private JPanel p;
	private String log;
	public ChangePanelListener(JFrame _f,JPanel _p,String _log){
		f = _f;
		p = _p;
		log = _log;
	}
	public void actionPerformed(ActionEvent e){
		f.setContentPane(p);
		f.setVisible(true);
		System.out.println(log);
		
	}
}

class gameSetting{
	int player = 2;
	String map = "NTU";
	int money = 200000;
	String name[] = new String[4];
	
	void setName(int i,String n){
		name[i] = n;
	}
	
	void setPlayer(int n){
		player = n;
	}
	
	void setMoney(int n){
		money = n;
	}
	
	void setMap(String n){
		map = n;
	}
	
	void showInfo(){
		System.out.println("Player="+player);
		System.out.println("Money="+money);
		System.out.println("Map="+map);
		for(int i=0;i<4;i++){
			System.out.println(name[i]);
		}
	}
	
}

class TextListener implements ActionListener{
	JTextField j;
	gameSetting set;
	int i;
	public TextListener(int _i,gameSetting _set,JTextField _j){
		i = _i;
		set = _set;
		j = _j;
	}
	public void actionPerformed(ActionEvent e){
		set.setName(i,j.getText());
		set.showInfo();
	}
}

class MapSelectingListener implements ActionListener{
	gameSetting set;
	int mode;
	JTextField[] j;
	JLabel[] l,mp;
	public MapSelectingListener(gameSetting _set,int _mode,JTextField[] _j,JLabel[] _l,JLabel[] _mp){
		set = _set;
		mode = _mode;
		j = _j;
		l = _l;
		mp = _mp;
		
	}
	public void actionPerformed(ActionEvent e){
		JComboBox cb = (JComboBox)e.getSource();
		String ns = (String)cb.getSelectedItem();
		int i = 2;
		int k = 200000;
		switch(mode){
			case 1:
				if(ns=="2"){
					i = 2;
					j[0].setVisible(true);
					j[1].setVisible(true);
					j[2].setVisible(false);
					j[3].setVisible(false);
					l[0].setVisible(true);
					l[1].setVisible(true);
					l[2].setVisible(false);
					l[3].setVisible(false);
					
				}else if(ns=="3"){
					i = 3;
					j[0].setVisible(true);
					j[1].setVisible(true);
					j[2].setVisible(true);
					j[3].setVisible(false);
					l[0].setVisible(true);
					l[1].setVisible(true);
					l[2].setVisible(true);
					l[3].setVisible(false);
				}else{
					i = 4;
					j[0].setVisible(true);
					j[1].setVisible(true);
					j[2].setVisible(true);
					j[3].setVisible(true);
					l[0].setVisible(true);
					l[1].setVisible(true);
					l[2].setVisible(true);
					l[3].setVisible(true);
				}
				set.setPlayer(i);
				break;
			case 2:
				if(ns=="200000"){
					k = 200000;
				}else if(ns=="300000"){
					k = 300000;
				}else{
					k = 400000;
				}
				set.setMoney(k);
				break;
			case 3:
				for(int o=0;o<mp.length;o++){
					if(o==cb.getSelectedIndex()){
						mp[o].setVisible(true);
					}else{
						mp[o].setVisible(false);
					}
				}
				set.setMap((String)cb.getSelectedItem());
				break;
		}
		set.showInfo();
		//System.out.println("new selection = " + newSelection);
	}
}



/* 結束遊戲 */
class EndingListener implements ActionListener{
	public void actionPerformed(ActionEvent e){
		System.out.println("結束遊戲");
		System.exit(0);
	}
}

/* 顯示遊戲資訊 */
class DevListener implements ActionListener{
	public void actionPerformed(ActionEvent e){		
		JOptionPane.showMessageDialog(new JFrame(),
		 "<html><center><h1>"+system.name+" "+system.versionName+"</h1>一個基於Java的大富翁遊戲。"
		+"<h2>版本</h2>"+system.version+" (內部測試)<br>"
		+"<h2>程式開發人員</h2>"
		+"<p>鍾健成<br><a href='mailto:b98902126@ntu.edu.tw'>b98902126@ntu.edu.tw</a></p>"
		+"<p>李卿澄<br><a href='mailto:b97501046@ntu.edu.tw'>b97501046@ntu.edu.tw</a></p>"
		+"<p><br><a href='mailto:@ntu.edu.tw'>@ntu.edu.tw</a></p>"
		+"<br><br>"
		+"國立臺灣大學資訊工程學系<br>"
		+"2010物件導向程式設計期末專案<br>"
		+"</center></html>","關於 "+system.name,JOptionPane.PLAIN_MESSAGE);    				
	}
}

class NewGameListener implements ActionListener{
	gameSetting set;
	JFrame f;
	JPanel p;
	JPanel tmp;
	JLabel text;
	JButton start;
	JProgressBar pb;
	Map map;
	public NewGameListener(gameSetting _set,JFrame _f,JPanel _p,Map _map){
		set = _set;
		f = _f;
		p = _p;
		tmp = new JPanel();
		text = new JLabel("遊戲初始化中...");
		pb = new JProgressBar(0,100);
		start = new JButton("進入遊戲");
		start.setVisible(false);
		tmp.add(text);
		tmp.add(pb);
		tmp.add(start);
		map = _map;
	}
	public void actionPerformed(ActionEvent e){
		//顯示初始化頁面
		f.setContentPane(tmp);
		f.setVisible(true);
		
		pb.setValue(0);
		pb.setStringPainted(true);
		
		//開始初始化！
		map.initgui(set,pb);
		System.out.println("Init OK");
		//初始化完成，顯示遊戲主畫面!
		start.setVisible(true);
		text.setText("初始化完成！");
		start.addActionListener(new ChangePanelListener(f,p,"進入主畫面"));
	
	}
}

/* 當滑鼠移過按鍵的動作 */
class MouseMoveListener implements MouseListener{
	private JLabel j;
	private String textIn;
	private String textOut; 
	/* 建構子 */
	public MouseMoveListener(JLabel _j,String _textIn,String _textOut){
		j = _j;
		textIn = _textIn;
		textOut = _textOut;
	}
	/* 移入區域 */
	public void mouseEntered(MouseEvent e){
		j.setText(textIn);		
	}
	/* 移出 */
	public void mouseExited(MouseEvent e){
		j.setText(textOut);
	}
	
	/* 被按過 */
	public void mouseClicked(MouseEvent e){
		/* 預設沒有動作 */
	}
	/* 壓下*/
	public void mousePressed(MouseEvent e){
		/* 預設沒有動作 */
	}
	/* 放開 */
	public void mouseReleased(MouseEvent e){
		/* 預設沒有動作 */
	}
	
}
